﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using Util;
public partial class fileList : System.Web.UI.Page
{
    private int PageIndex { get { return (int)ViewState["index"]; } set { ViewState["index"] = value; } }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Util.UtilWeb.CheckRights(this);
            PageIndex = 1;
            LoadView();
            LoadData();
        }
    }

    private void LoadView()
    {
        var lbl = Master.FindControl("lblTitle") as Label;
        lbl.Text = "文件列表";
        new BllAss().InitListControl(ddlAss);
    }

    private Func<tb_file, bool> GetFunc()
    {
        var func = UtilExp.ExprTrue<tb_file>();
        if (ddlAss.SelectedValue != "0")
        {
            func = func.ExprAnd(c => c.ass.Equals(ddlAss.SelectedValue));
        }
        return func.Compile();
    }

    private void LoadData()
    {
        BllFile bll = new BllFile();

        var func = GetFunc();
        webPager.RecordCount = bll.GetCount(func);

        var tp = bll.GetList(func, true, c => c.time, PageIndex, webPager.PageSize);

        var data = from c in tp
                   select new
                   {
                       id = c.id,
                       name = UtilStr.CutStr(c.name, 14),
                       ass = new BllAss().GetSingle(b => b.id.ToString().Equals(c.ass)).name,

                   };
        dl.DataSource = data;
        dl.DataBind();
    }
    protected void webPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        PageIndex = e.NewPageIndex;
        LoadData();
    }

    protected void dl_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string cmd = e.CommandName;
        int id = Convert.ToInt32(e.CommandArgument);
        BllFile bll = new BllFile(this);
        if (cmd == "edit")
        {
            Response.Redirect("fileEdit.aspx?id=" + id.ToString());
        }
        if (cmd == "delete")
        {
            if (bll.Delete(id))
                LoadData();
        }
    }

    protected void ddlAss_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadData();
    }
}